<?php
	include_once('connexion.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by TEMPLATED
http://templated.co
Released for free under the Creative Commons Attribution License

Name       : Assembly 
Description: A two-column, fixed-width design with dark color scheme.
Version    : 1.0
Released   : 20140330

-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Kilbet - The first social betting platform</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="http://fonts.googleapis.com/css?family=Didact+Gothic" rel="stylesheet" />
<link href="styles/default.css" rel="stylesheet" type="text/css" media="all" />
<link href="styles/fonts.css" rel="stylesheet" type="text/css" media="all" />
<link rel="shortcut icon" type="image/x-icon" href="pictures/logo_kilbet.png" />

<!--[if IE 6]><link href="default_ie6.css" rel="stylesheet" type="text/css" /><![endif]-->

</head>

<body>
<?php include_once('header.php');?>

<div id="big_content">
	
	<div id="contests_menu" class "container">
		<nav id="contests_inside_menu" class="inside_menu">
			<ul>
				<li class="title_inside_menu">Concours</li>
				<li <?php if($current=="/kilbet/listcontest.php" && (!isset($_GET['view']) || $_GET['view']=="overview")) echo 'class="tab_inside_menu_active"'; else echo 'class=tab_inside_menu';?>><a href="?view=overview">Tous les concours</a></li>
				<li <?php 
				if(isset($userConnected))
				{
					if($current=="/kilbet/listcontest.php" && isset($_GET['view']) && $_GET['view']=="mycontests") echo 'class="tab_inside_menu_active"'; else echo 'class=tab_inside_menu';?>><a href="?view=mycontests">Mes Concours</a></li>
				<?php
				}
				?>
				<div style="clear: both;"></div>
			</ul>
		</nav>
		<div style="clear: both;"></div>
	</div>
	
	<div id="content">
	
		<div id="intro" class="container">
		<br/>
				<span class="byline">Découvrez ici l'ensemble des concours disponibles sur Kilbet : concours publics, concours privés, vos concours.</span>
			</div>
			<div style="clear: both;"></div>
			
		<div id="lists_contests_content" class="container">

		<!--Affichage du menu latéral relatif à la page listcontest-->
	
	
		
	<?php
	if(!isset($_GET['view']) || (isset($_GET['view']) && $_GET['view']=="overview"))
	{
		?>
		<div id="section_list_contests">
		
		<?php
		//Affichage de la liste des sports supportés, avec les différents sportEvents
			//Si pas de user connecté : on affiche juste le nombre de concours public existant dans chaque catégorie
			//Si user connecté : on donne une vue sur les concours privés existants avec mise en évidence des concours auxquels le user participe
		
		//On liste tous les sports supportés par Kilbet : chacun d'eux est cliquable => redirection vers une vue avec présentation du sport
		$listSports = $sportsManager->listSports();
		foreach($listSports as $sport)
		{
				echo '<div class="list_contest_sport">';
				echo '<span class="sport_name"><a href="?view=sport&sportid='.$sport->id().'">'.$sport->name().'</a></span><br />';
				echo '<span class="sport_picture"><a href="?view=sport&sportid='.$sport->id().'"><img src="pictures/'.$sport->name().'.png" alt="'.$sport->name().'"/></a></span>';
				//On liste tous les sportsEvents pour chaque sport : chacun d'eux est cliquable => redirection vers une vue avec présentation du sportsEvent
				$listSportsEvents = $sportsEventManager->listEventsBySports($sport);
				echo '<ul>';
				foreach($listSportsEvents as $sportEvent)
				{
					//Pour chaque sportsEvent, on indique entre parenthèse le nombre de concours en cours => Si aucun user connecté : concours publics uniquement
					if(isset($userConnected)) $nbContests = count($contestManager->getListContests($sport,$sportEvent));
					else $nbContests = count($contestManager->getListContests($sport, $sportEvent, null, 1));
					echo '<li class="event_name"><a href="?view=sport&sportid='.$sport->id().'&event='.$sportEvent->id().'">'.$sportEvent->shortName().'</a> ('.$nbContests.')</li>';
				}
				echo '</ul>';
			echo '</div>';
		}
		?>
		<div style="clear: both;"></div>
		</div>
		<?php
	}
	elseif(isset($_GET['view']) && $_GET['view']=="mycontests")
	{
		if(!isset($userConnected)) header('Location: index.php');
		include_once('mycontests.php');
	}
	elseif(isset($_GET['view']) && $_GET['view']=="sport")
	{
		//Affichage de la page de présentation du sport : si aucun sportsEvent sélectionné, on affiche tous les sportsEvents ; sinon on affiche uniquement le sportsEvent
			//Si pas de user connecté : on affiche juste les concours publics existant
			//Si user connecté : on affiche tout
		?>
		<div id="section_list_contests">
		<table id="board" summary="contests_board" class="tab">
	<thead>
		<tr>
			<th id="statusContest_col">Statut concours</th>
			<th id="sport_col">Sport</th>
			<th id="event_col">Compétition</th>
			<th id="contestName_col">Concours</th>
			<?php if(isset($userConnected)) { ?><th id="statusPlayer_col">Statut</th><?php }?>
			<th id="nbPlayers_col">Nombre de joueurs</th>
			<?php if(isset($userConnected)) { ?><th id="admin_col">Admin</th><?php }?>
			<th id="creator_col">Créateur</th>
		</tr>
	</thead>
	<tfoot>
		<tr>
			<td colspan="<?php if(isset($userConnected)) echo '8'; else echo '6';?>">Liste des concours</td>
		</tr>
	</tfoot>
	<tbody>
		<?php
		if(!isset($_GET['sportid'])) header('Location: listcontest.php');
		$sport = $sportsManager->get((int) $_GET['sportid']);
		echo '<h1>'.$sport->name().'</h1>';
		echo '<p>'.$sport->description().'</p>';
		$listSportsEvents = $sportsEventManager->listEventsBySports($sport);
		foreach($listSportsEvents as $sportsEvent)
		{
			if(isset($_GET['event']) && $sportsEvent->id()==$_GET['event'])
			{
				if(isset($userConnected)) $listContests = $contestManager->getListContests($sport,$sportsEvent, null, 0);
				else $listContests = $contestManager->getListContests($sport,$sportsEvent, null, 1);
				if(count($listContests) == 0)
				{
					?>
					<tr>
						<td colspan="<?php if(isset($userConnected)) echo '8'; else echo '6';?>">Aucun concours</td>
					</tr>
					<?php
				}
				else
				{
					foreach($listContests as $contest)
					{
						$nbPlayers = count($contestManager->getListPlayers($contest, 1));
						?>
						<tr>
							<td><?php echo $contestManager->getStatus($contest);?></td>
							<td><?php echo $sportsManager->get((int) $sportsEventManager->get((int) $eventManager->get((int) $contest->event_id())->sportsEvent_id())->sport_id())->name();?></td>
							<td><?php echo $sportsEventManager->get((int) $eventManager->get((int) $contest->event_id())->sportsEvent_id())->shortName();?></td>
							<td><a href="contest.php?contestid=<?php echo $contest->id();?>" alt="Consulter ce concours"><?php echo $contest->name();?></a></td>
							<?php if(isset($userConnected)) {?><td><?php echo $userManager->getStatusPlayer($userConnected, $contest)[1];?></td><?php }?>
							<td><?php echo count($contestManager->getListPlayers($contest, 1));?></td>
							<?php if(isset($userConnected)) { ?><td><?php if($userManager->isAdmin($userConnected, $contest)) echo "Oui"; else echo "Non"; ?></td><?php }?>
							<td><?php echo $contestManager->getCreator($contest)->login();?></td>
						</tr>
					<?php
					}
				}
			}
		}
		if(!isset($_GET['event']))
		{
			if(isset($userConnected)) $listContests = $contestManager->getListContests($sport,null, null, 0);
			else $listContests = $contestManager->getListContests($sport,null, null, 1);
			if(count($listContests) == 0)
			{
				?>
				<tr>
					<td colspan="<?php if(isset($userConnected)) echo '8'; else echo '6';?>">Aucun concours</td>
				</tr>
				<?php
			}
			else
			{
				foreach($listContests as $contest)
				{
					$nbPlayers = count($contestManager->getListPlayers($contest, 1));
					?>
					<tr>
						<td><?php echo $contestManager->getStatus($contest);?></td>
						<td><?php echo $sportsManager->get((int) $sportsEventManager->get((int) $eventManager->get((int) $contest->event_id())->sportsEvent_id())->sport_id())->name();?></td>
						<td><?php echo $sportsEventManager->get((int) $eventManager->get((int) $contest->event_id())->sportsEvent_id())->shortName();?></td>
						<td><a href="contest.php?contestid=<?php echo $contest->id();?>" alt="Consulter ce concours"><?php echo $contest->name();?></a></td>
						<?php if(isset($userConnected)) {?><td><?php echo $userManager->getStatusPlayer($userConnected, $contest)[1];?></td><?php }?>
						<td><?php echo count($contestManager->getListPlayers($contest, 1));?></td>
						<?php if(isset($userConnected)) { ?><td><?php if($userManager->isAdmin($userConnected, $contest)) echo "Oui"; else echo "Non"; ?></td><?php }?>
						<td><?php echo $contestManager->getCreator($contest)->login();?></td>
					</tr>
				<?php
				}
			}
		}
		?>
		</tbody>
		</table>
		</div>
		<?php
	}
	elseif(isset($_GET['view']) && $_GET['view']=="searchcontest")
	{
		include('search_contest.php');
	}
	?>
		<div style="clear: both;"></div>
		</div>
	</div>
	</div>
<footer>
	<?php include_once('footer.php');?>
</footer>

</body>
</html>